<template>
  <div class="page-container">
    <pf-table
      ref="pfTable"
      v-model="form"
      :columns="columns"
      :form-items="formItems"
      @selection-change="handleSelectionChange"
      @get-table-data="getTableData"
    >
      <template #handle-area>
        <el-button type="primary" @click="addHandle">新增账号</el-button>
        <el-button type="danger" @click="bathDeleteHandle">删除</el-button>
      </template>
      <template #statusStr="{row}">
        <el-tag v-if="row.status === 0" type="success">{{ row.statusStr }}</el-tag>
        <el-tag v-if="row.status === 1" type="danger">{{ row.statusStr }}</el-tag>
      </template>
      <template #handle="{row}">
        <el-button type="text" size="small" @click="editHandle(row)">编辑</el-button>
        <el-button type="text" size="small" :class="row.status === 0 ? 'danger-text-button': ''" @click="editStatus(row)">{{ row.status === 0 ? '禁用':'启用' }}</el-button>
        <el-button type="text" class="danger-text-button" size="small" @click="resetHandle(row)">重置密码</el-button>
        <el-button type="text" class="danger-text-button" size="small" @click="deleteHandle(row)">删除</el-button>
      </template>
    </pf-table>
    <AddUser
      v-if="showUser"
      :id="userId"
      :type="type"
      @close="closeHandle"
      @confirm="confirmHandle"
    />
  </div>
</template>

<script>
import { getUserList, deleteUser, editUserStatus, resetPassword } from '@/api/system'
import { mapGetters } from 'vuex'
import AddUser from './AddUser.vue'

export default {
  name: 'UserList',
  components: {
    AddUser
  },
  data() {
    return {
      type: 'add',
      showUser: false,
      form: {},
      loading: false,
      userId: '',
      selectionList: []
    }
  },
  computed: {
    ...mapGetters([
      'currentEnterprise',
      'currentSite'
    ]),
    columns() {
      const attrs = { 'min-width': '150px' }
      return [
        { attrs: { type: 'selection' }},
        { label: '账号名称', prop: 'account', attrs },
        { label: '角色', prop: 'roleName', attrs },
        { label: '姓名', prop: 'userName', attrs },
        { label: '手机号', prop: 'mobile', attrs },
        { label: '性别', prop: 'sexStr', attrs },
        { label: '账号状态', slot: 'statusStr', attrs },
        { label: '操作', slot: 'handle', attrs: { width: '240px', fixed: 'right' }}
      ]
    },
    formItems() {
      return [
        { type: 'input', label: '账号名称', prop: 'account' },
        { type: 'input', label: '姓名', prop: 'userName' },
        { type: 'input', label: '角色', prop: 'roleName' },
        {
          type: 'date',
          label: '创建时间',
          prop: 'dateRange',
          span: 8,
          attrs:
            {
              type: 'datetimerange'
            }
        }
      ]
    }
  },

  methods: {

    getTableData({ pageNo, pageSize, ...formQuery }, done) {
      this.loading = true
      const [createStartTime = '', createEndTime = ''] = formQuery?.dateRange || []
      const data = {
        pageNo,
        pageSize,
        createStartTime,
        createEndTime,
        ...formQuery
      }
      getUserList(data).then(res => {
        this.loading = false
        done({ data: res.data || [], total: res.count || 0 })
      }).catch(() => {
        done({ data: [], total: 0 })
        this.loading = false
      })
    },

    // 选择
    handleSelectionChange(selection) {
      this.selectionList = selection
    },

    // 新增
    addHandle() {
      this.type = 'add'
      this.userId = ''
      this.showUser = true
    },

    // 编辑
    editHandle(row) {
      this.type = 'edit'
      this.userId = row.id
      this.showUser = true
    },

    // 重置密码
    resetHandle(row) {
      const data = {
        accountId: row.id
      }
      this.$confirm('确定要重置该用户的密码吗，重置后的密码为123456', '提示', {
        type: 'warning'
      }).then(() => {
        resetPassword(data).then(() => {
          this.$message.success('重置密码成功')
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      })
    },

    // 修改状态
    editStatus(row) {
      const data = {
        id: row.id,
        status: row.status === 1 ? 0 : 1
      }
      let message = ''
      if (row.status === 0) {
        message = '确定禁用该账号吗？'
      } else {
        message = '确定启用该账号吗？'
      }
      this.$confirm(message, '提示', {
        type: 'warning'
      }).then(() => {
        editUserStatus(data).then(() => {
          this.$message.success('修改成功')
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      })
    },

    // 删除
    deleteHandle(row) {
      this.userId = row.id
      this.deleteUserHandle([this.userId])
    },

    // 批量删除
    bathDeleteHandle() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请先选择账号')
        return false
      }
      const idList = []
      this.selectionList.map(item => {
        idList.push(item.id)
      })
      this.deleteUserHandle(idList)
    },

    // 删除操作
    deleteUserHandle(idList) {
      const data = {
        idList
      }
      this.$confirm('确定删除该账号吗?', '提示', {
        type: 'warning'
      }).then(() => {
        deleteUser(data).then(() => {
          this.$message.success('删除成功')
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      }).catch(() => {})
    },

    // 关闭弹窗
    closeHandle() {
      this.showUser = false
    },

    // 确认
    confirmHandle() {
      this.$refs.pfTable.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped></style>
